// https://leetcode.cn/problems/kth-smallest-element-in-a-bst/
// Created by ade on 2022/9/8.
//
#include <iostream>
#include <vector>

using namespace std;

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;

    TreeNode() : val(0), left(nullptr), right(nullptr) {}

    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}

    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {};

};

class Solution {
public:
    vector<int> res;

    int kthSmallest(TreeNode *root, int k) {
        dfs(root);
        for (auto i : res) {
            cout << i << ",";
        }
        cout << endl;
        return res[k - 1];
    }

    void dfs(TreeNode *root) {
        if (!root)return;
        dfs(root->left);
        res.push_back(root->val);
        dfs(root->right);
    }

    TreeNode *init2() {
        TreeNode *n1 = new TreeNode(1);
        TreeNode *n2 = new TreeNode(2);
        TreeNode *n3 = new TreeNode(3);
        TreeNode *n4 = new TreeNode(4);
        n3->right = n4;
        n3->left = n2;
        n2->left = n1;
        return n3;
    }
};

int main() {
    Solution so;
    TreeNode *head = so.init2();
    cout << so.kthSmallest(head, 1);
    return 0;
}